package com.ry.est.controller;

import com.ry.est.constants.ResultEnum;
import com.ry.est.dto.CommonResult;
import com.ry.est.entity.Users;
import com.ry.est.exception.BussinessException;
import com.ry.est.service.UserService;
import com.ry.est.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ry
 * @since 2021-11-01 17:05
 **/
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private UserService userService;

    @GetMapping("/test1")
    public String test1(){
        return "test ok";
    }

    @GetMapping("/getUserById1")
    public Users getById(@RequestParam("id") Long id){
        return userService.findById(id);
    }

    @GetMapping("/getUserById2")
    public CommonResult<Users> test2(@RequestParam("id") Long id){
        Users byId = userService.findById(id);

        CommonResult<Users> usersCommonResult = new CommonResult<>();
        usersCommonResult.setCode("0");
        usersCommonResult.setMsg("操作成功");
        usersCommonResult.setData(byId);


        return ResultUtil.success(byId);
    }


    @GetMapping("/exception")
    public void testException(){
        throw new BussinessException("-1","登录异常");
    }

    @GetMapping("/exception2")
    public void testException2(){
        throw new BussinessException(ResultEnum.COMMON_EXCEPTION);
    }
}
